package com.bjtu.literatureclient.controller.team;

import com.bjtu.literatureclient.domain.UserTeam;
import com.bjtu.literatureclient.domain.team.Team;
import com.bjtu.literatureclient.domain.user.User;
import com.bjtu.literatureclient.service.team.ITeamService;
import com.bjtu.literatureclient.service.user.IUserService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/team")
public class TeamController {

	/**
	 *   @RequiresPermissions(value = {"superAdmin","selfAdmin"})  、@RequireRoles用于控制权限
	 *   superAdmin
	 *   selfAdmin
	 *   teamAdmin
	 *   superUser
	 *   highUser
	 *   middleUser
	 *   primaryUser
	 *
	 *   这几个权限分别对应文档的不同权限
	 *	 这些权限用于控制后端接口的权限
	 */


	@Autowired
	private ITeamService teamService;
	@Autowired
	private IUserService userService;


	@RequestMapping(value = "/getNewTeamNo", method = RequestMethod.POST)
	public String getNewTeamNo() {
		String teamNo = teamService.getNewTeamNo();
		Gson gson = new Gson();
		return gson.toJson(teamNo);
	}


	@RequestMapping(value = "/addTeamAndAdmin", method = RequestMethod.POST)
	public String addTeamAndAdmin(@RequestBody UserTeam userTeam) {
		long l = System.currentTimeMillis();
		Date date = new Date(l);
		SimpleDateFormat createTimeFormat = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat updateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String createTime = createTimeFormat.format(date);
		String updateTime = updateTimeFormat.format(date);

		// 创建管理员实体
		User user = new User();
		user.setUserNo("U"+userTeam.getUserNo().substring(1));
		user.setPassword(userTeam.getPassword());
		user.setUserName(userTeam.getUserName());
		user.setUserTeamNo(userTeam.getTeamNo());
		user.setiDNo(userTeam.getiDNo());
		user.setUserPhone(userTeam.getUserPhone());
		user.setUserEmail(userTeam.getUserEmail());
		user.setUserRole(userTeam.getUserRole());
		user.setDate(createTime);
		user.setDateTime(updateTime);
		user.setUserState("1"); // 代表第一次登陆

		// 创建新团队实体
		Team team = new Team();
		team.setTeamNo(userTeam.getTeamNo());
		team.setUserName(userTeam.getUserName());
		team.setTeamName(userTeam.getTeamName());
		team.setCurrentNum(1);
		team.setMaxNum(userTeam.getMaxNum());
		team.setUserEmail(userTeam.getUserEmail());
		team.setTeamEmail(userTeam.getTeamEmail());
		team.setDate(createTime);
		team.setDateTime(updateTime);
		team.setTeamState("0");

		int flag = teamService.update(team);
		int flag1 = userService.insert(user);
		Map<String, String> resultMap = new HashMap<String, String>();
		if (flag == 1 && flag1 == 1) {
			resultMap.put("data", "success");
		} else {
			resultMap.put("data", "error");
		}

		Gson gson = new Gson();
		return gson.toJson(resultMap);
	}


	@RequestMapping(value = "/query/{param}/{value}", method = RequestMethod.GET)
	public String query(@PathVariable("param") String param, @PathVariable("value") String value) {
		Team team = new Team();
		switch (param) {
		case "teamState":
			team.setTeamState(value);
			break;
		}
		List<Team> teamList = this.teamService.query(team);
		//System.out.println(teamList);
		Gson gson = new Gson();
		return gson.toJson(teamList);
	}


	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(@RequestBody Team team) {
		
		Map<String, String> resultMap = new HashMap<String, String>();
		if (!StringUtils.isEmpty(team.getTeamNo())) {
			int flag = this.teamService.update(team);
			if (flag == 1) {
				resultMap.put("code", "200");
				resultMap.put("data", "success");
			}
		} else {
			resultMap.put("code", "-1");
			resultMap.put("data", "team update error");
		}
		Gson gson = new Gson();
		return gson.toJson(resultMap);
	}

	// 检查团队邮箱
	@RequestMapping(method = RequestMethod.POST, value = "checkTeamEmail")
	public String checkTeamEmail(@RequestBody Team team) {
		String result = teamService.checkTeamEmail(team.getTeamEmail());
		Map<String, String> resultMap = new HashMap<String, String>();
		resultMap.put("data", result);
		Gson gson = new Gson();
		return gson.toJson(resultMap);
	}


}
